﻿package com.ostrich.manpower.service.impl;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.StringUtil;
import com.jeecg.entity.SysUser;
import com.jeecg.service.SysUserService;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrCheckinlog;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.bean.TostHrPostinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.common.StringUtils;
import com.ostrich.manpower.service.TostHrBaseinfoDao;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;

/**
 * @author wangyinan
 * @param <E>
 * @brief 模块功能简单描述
 * @details 模块功能详细描述
 */
@Service("tostHrBaseinfoService")
public class TostHrBaseinfoService<T> extends BaseService<T> {
	private final static Logger log = Logger
			.getLogger(TostHrBaseinfoService.class);

	@Autowired
	private TostHrBaseinfoDao<T> dao;

	public TostHrBaseinfoDao<T> getDao() {
		return dao;
	}
	@Autowired
	TostHrDepartinfoService<T>  tosthrdepartinfoservice;
	@Autowired
	TostHrPostinfoService<T>  tothrpostinfoservice;
	@Autowired
	SysUserService<T> sysUserService;
	@Autowired
	TostHrPostinfoService<T>  tosthrpostinfoservice;

	public List<T> queryByhrDepart(String departmenma){
		return getDao().queryByhrDepart(departmenma);
	}

	
	public TostHrBaseinfo addAndReturn(TostHrBaseinfo baseInfo){
		getDao().add((T) baseInfo);
		return baseInfo;
	}
	/****
	 * 查询基础表中的下月过生日的人。根据身份证号的第11位月份来获取
	 * 
	 * @return
	 */
	public List<T> queryBymonthbir() {
		List<T> monthbirpopel = getDao().queryBymonthbir();
		return monthbirpopel;
	}

	public List<T> queryAll() {
		return getDao().queryAll();
	}
	public T queryById(int id){
		return getDao().queryById(id);
	}
	
	public List<T> queryAllById(int id){
		return dao.queryAllById(id);
	}
	public void setBaseInfoBySheet(HSSFSheet sheet) throws Exception{
		//List<TostHrMealaddinfo> infos =new ArrayList<TostHrMealaddinfo>();
	    HSSFRow row = null;
	    String  strs="";//保存重复人的idcade
	    int s=0;//保存次数
	    int colnum = 19;
	    //公司
	    Map<String, TostSysParaminfo> cpmap = ParaminfoUtil.getParamKeyNameByPid(1);
	    //部门
	    Map<String, TostHrDepartinfo> dpmap = tosthrdepartinfoservice.queryDepartAllMapKeyName();
	    //任职岗位
	    Map<String, TostHrPostinfo> posmap = tosthrpostinfoservice.queryposMapKeyName();
	    //人员等级
	    Map<String, TostSysParaminfo> pgrd = ParaminfoUtil.getParamKeyNameByPid(5);
	    //人员类别
	    Map<String, TostSysParaminfo> pertype = ParaminfoUtil.getParamKeyNameByPid(11);
	    
		SysUser usr= new SysUser();
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
	         row = sheet.getRow(j);
	         TostHrBaseinfo tbase= new TostHrBaseinfo();
	         for(int i=0;i<colnum;i++ ){
	        	 String str = ImportUtil.getCellValue(row.getCell(i));
	        	 if(i==0)//员工编号
	        		 tbase.setHrCode(str);
	        	 else if(i==1){//公司
	        		 TostSysParaminfo tsysp=cpmap.get(str);
	        		 tbase.setHrCompany((tsysp!=null)?tsysp.getId().toString():"");
	        	 }else if(i==2){//部门
	        		 TostHrDepartinfo dpinfo =  dpmap.get(str);
	        		 tbase.setHrDepart((dpinfo!=null)?dpinfo.getId().toString():"");
	        	 }else if(i==3){//姓名
	        		tbase.setHrName(str);
	        		//增加用户登陆表信息内容
	        		/*if(str!=null && str.length()>0){
		        		String username=StringUtils.chinaToPingyi(str);
		        		int isexit = sysUserService.getUserCountByEmail(username);
		        		if(isexit<=0){
		        			usr=(SysUser)sysUserService.adduser(username,str);
		        		}
	        		 }*/
	        	 }else if(i==4){//任职岗位
	        		 TostHrPostinfo posjob =  posmap.get(str);
	        		 tbase.setHrJobs((posjob!=null)?posjob.getId().toString():"");
	        	 }else if(i==5){//人员级别
	        		 TostSysParaminfo Grade=pertype.get(str);
	        		 tbase.setHrGrades((Grade!=null)?Grade.getId().toString():"");
	        	 }else if(i==6){//职务等级
	        		 TostSysParaminfo Leve=pgrd.get(str);
	        		 tbase.setHrLevel((Leve!=null)?Leve.getId().toString():"");
	        	 } else if(i==7){//入职时间
		        		 tbase.setHrEbtertime(str);
	        	 }else if(i==8){//调资时间
	        		 tbase.setHrWagtime(str);
	        	 }else if(i==9){//调整前工资标准
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tbase.setHrPresentstan(flstr); 
	        	 }else if(i==10){//工资标准
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tbase.setHrExesalry(flstr);
	        	 } else if(i==11){//提留工资
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tbase.setHrLeavemoney(flstr);
	        	 }else if(i==12){//绩效提成工资
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tbase.setHrPushmoney(flstr);
	        	 }else if(i==13){//进入目前公司时间
	        		 tbase.setHrPresentcomp(str);
	        	 }else if(i==14){//转正时间
	        		 tbase.setHrPositivetime(str);
	        	 }else if(i==15){//身份证号码
        			 tbase.setHrIdcard(str);
	        	 }else if(i==16){//工资银行
	        		 tbase.setHrBank(str);
	        	 }else if(i==17){//工资卡号
	        		 tbase.setHrBankid(str);
	        	 }else if(i==18){//购车购房借款
	        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
	        		 tbase.setHrLoadmoney(flstr);
	        	 }
	        	 
	         	}
	         if(tbase!=null && tbase.getHrName()!=null && tbase.getHrName().trim().length()>0){
	        	//根据员工编号查询是否有该员工
	        	 TostHrBaseinfo bslg= (TostHrBaseinfo) getBaseinfoByHrCode(tbase.getHrCode());
	        	 if(bslg!=null && bslg.getId()!=null){
	        		 tbase.setId(bslg.getId());
	        		 dao.update((T) tbase);
	        	 }else{
	        		 //tbase.setId(usr.getId());
	        		 dao.add((T) tbase);
	        	 }
	         }
	     }
	}
	
	//设置分页
public List<Map<String,Object>> getBaseinfoByPage(TostHrBaseinfo page) throws Exception{
	Integer rowCount = queryByCount(page);
	page.getPager().setRowCount(rowCount);
	List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>();
	Map<String,Object> mp = new HashMap<String,Object>();
	mp.put("pager", page.getPager());
	ls.add(mp);
	ls.addAll(dao.getBaseinfoByPage(page));
	return ls;
}
	
	/****
	 * 根据员工编号
	 * @param code
	 * @return
	 */
	public T getBaseinfoByHrCode(String code){
		return getDao().getBaseinfoByHrCode(code);
	}
	
	
//查询身份证号码。永远添加导入的时候判断是否有相同的人 //判断有没有重复的人
	public Boolean isHave(String hrIdcard){
		Integer ct =  getDao().isHaveperson(hrIdcard);
		if(ct!=null && ct>0)
			return true;
		else
			return false;
	}
	
	@Override
	public void delete(Object... ids) throws Exception{
		if(ids == null || ids.length < 1){
			return;
		}
		for(Object id : ids ){
			 TostHrBaseinfo bslg= (TostHrBaseinfo) queryById(id);
			 SysUser us = (SysUser) sysUserService.queryByEmail(StringUtils.chinaToPingyi(bslg.getHrName()));
			 if(us!=null){
				 Object[] uid = {us.getId()};
				 sysUserService.delete(uid);
			 }
			getDao().delete(id);
		}
	}
//查出身份证号
	public List<T> queryIDcadeByName(String name){
		return getDao().queryIDcadeByName(name);
		
	}
	//查询id
public int queryHridByname(String name){
	return getDao().queryHridByname(name);
}
//通过id查姓名
public String  queryHrnameByid(int hrid){
	return getDao().queryHrnameByid(hrid);
}
	
}
